﻿using HookupAppForm.Draw.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace HookupAppForm.Draw
{
    public class LocalDataHelper
    {
        //private static readonly string url_shapelist = "localapi/ShapeList";
        //private static readonly string url_shape_del = "localapi/ShapeDel";
        //private static readonly string url_floordrawingadd = "localapi/FloorDrawing";

        private static readonly string url_shapelist = "api/services/app/HKP_Draw/GetShapeList";
        private static readonly string url_shapeadd = "api/services/app/HKP_Draw/AddShap";
        private static readonly string url_shapeupdate = "api/services/app/HKP_Draw/UpdateShap";
        private static readonly string url_shape_del = "api/services/app/HKP_Draw/DelShape";

        private static readonly string url_floordrawingGet = "api/services/app/HKP_Draw/GetFloorDraw";
        private static readonly string url_floordrawingAdd = "api/services/app/HKP_Draw/AddFloorDraw";
        private static readonly string url_floordrawingUpdate = "api/services/app/HKP_Draw/UpdateFloorDraw";

        private static APIClient _Client;
        public static APIClient Client
        {
            get
            {
                return FormMain.Instance.client;
            }
            set { _Client = value; }
        }
 
        internal static List<CustomDrawInfo> GetShapeList(UserControl uc, string str_search_shapename)
        {
            try
            {
                var jstr = Client.Get(url_shapelist);

                var rstinfo = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(jstr);
                if (rstinfo == null || rstinfo.result == null || rstinfo.result.Count == 0) return new List<CustomDrawInfo>();


                var rstlst = new List<CustomDrawInfo>();
                uc.Dispatcher.Invoke(delegate
                {
                    foreach (var itemd in rstinfo.result)
                    {
                        var item = new CustomDrawInfo();

                        // "name": "string",
                        //"imgbase64": "string3333",
                        //"binddatatype": "string",
                        //"binddataprop": "string",
                        //"comparesign": "string",
                        //"comparevalue": "string",
                        //"ext1": "string",
                        //"ext2": "string",
                        //"ext3": "string",
                        //"eleListJsonStr": "string",
                        //"id": "421ca663-0fcd-4f07-503a-08d99cdcb3aa"

                        item.BindDataProperty = dynToStr(itemd.binddataprop);
                        item.BindDataType = dynToStr(itemd.binddatatype);
                        item.BindDataVal = dynToStr(itemd.comparevalue);
                        item.CompareSign = dynToStr(itemd.comparesign);
                        item.EleListJsonStr = dynToStr(itemd.eleListJsonStr);
                        item.ShapeName = dynToStr(itemd.name);
                        item.uid = dynToStr(itemd.id);
                        var ht = 100;
                        int.TryParse(dynToStr(itemd.ext1), out ht);
                        if (ht == 0) ht = 100;
                        item.ShowHeight = ht;

                        var bstr = dynToStr(itemd.imgbase64);
                        if (!string.IsNullOrWhiteSpace(bstr)) item.PreviewImg = Convert.FromBase64String(bstr);

                        if (!string.IsNullOrWhiteSpace(item.EleListJsonStr))
                        {
                            try
                            {
                                item.EleList = Newtonsoft.Json.JsonConvert.DeserializeObject<ObservableCollection<DrawEleInfo>>(item.EleListJsonStr);
                            }
                            catch (Exception)
                            {

                            }
                        }
                        rstlst.Add(item);
                    }
                });

                for (int i = 0; i < rstlst.Count; i++)
                {
                    rstlst[i].NO = i + 1;
                }
                return rstlst;
            }
            catch (Exception)
            {
                return new List<CustomDrawInfo>();
            }
        }
        internal static FloorDrawingInfo GetFloorDrawingInfo(UserControl uc, string key)
        {
            try
            {
                var jstr = Client.Get(url_floordrawingGet + "?id=" + key);

                var rstinfo = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(jstr);
                if (!((bool)rstinfo.success)) return null;


                FloorDrawingInfo item = new FloorDrawingInfo();
                uc.Dispatcher.Invoke(delegate
                {
                    var itemd = rstinfo.result;
                    item.uid = dynToStr(itemd.floorid);
                    item.DrawJsonStr = dynToStr(itemd.drawjson);
                    item.id = int.Parse(dynToStr(itemd.id));
                    if (!string.IsNullOrWhiteSpace(item.DrawJsonStr))
                    {
                        try
                        {
                            item.DrawDetail = Newtonsoft.Json.JsonConvert.DeserializeObject<FDInfo>(item.DrawJsonStr);
                        }
                        catch (Exception ee)
                        {

                        }
                    }

                });

                return item;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        internal static bool AddShape(CustomDrawInfo info)
        {
            try
            {
                if (info.EleList != null) info.EleListJsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(info.EleList);

                var info2 = new
                {
                    name = info.ShapeName,
                    imgbase64 = info.PreviewImg == null ? "" : Convert.ToBase64String(info.PreviewImg),
                    // imgbase64 = "",
                    binddatatype = info.BindDataType,
                    binddataprop = info.BindDataProperty,
                    comparesign = info.CompareSign,
                    comparevalue = info.BindDataVal,
                    eleListJsonStr = info.EleListJsonStr,
                    id = info.uid,
                    ext1 = info.ShowHeight,
                    ext2 = "",
                    ext3 = "",
                };
                var jstr = Client.Post(url_shapeadd, Newtonsoft.Json.JsonConvert.SerializeObject(info2));
                var rstinfo = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(jstr);
                return (bool)rstinfo.success;
            }
            catch (Exception)
            {
                return false;
            }
        }
        internal static bool UpdateShape(CustomDrawInfo info)
        {
            try
            {
                if (info.EleList != null) info.EleListJsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(info.EleList);

                var info2 = new
                {
                    name = info.ShapeName,
                    imgbase64 = info.PreviewImg == null ? "" : Convert.ToBase64String(info.PreviewImg),
                    binddatatype = info.BindDataType,
                    binddataprop = info.BindDataProperty,
                    comparesign = info.CompareSign,
                    comparevalue = info.BindDataVal,
                    eleListJsonStr = info.EleListJsonStr,
                    id = info.uid,
                    ext1 = info.ShowHeight,
                    ext2 = "",
                    ext3 = "",
                };
                var jstr = Client.Put(url_shapeupdate, Newtonsoft.Json.JsonConvert.SerializeObject(info2));
                var rstinfo = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(jstr);
                return (bool)rstinfo.success;
            }
            catch (Exception)
            {
                return false;
            }
        }
        internal static bool AddFloorDrawInfo(FloorDrawingInfo info)
        {
            try
            {
                if (info.DrawDetail != null) info.DrawJsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(info.DrawDetail);

                var info2 = new
                {
                    floorid = info.uid,
                    drawjson = info.DrawJsonStr,
                    id = 0,
                };
                var jstr = Client.Post(url_floordrawingAdd, Newtonsoft.Json.JsonConvert.SerializeObject(info2));
                var rstinfo = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(jstr);
                var ok = (bool)rstinfo.success;
                if (!ok) return false;

                info.id = (int)rstinfo.result.id;
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        internal static bool UpdateFloorDrawInfo(FloorDrawingInfo info)
        {
            try
            {
                if (info.DrawDetail != null) info.DrawJsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(info.DrawDetail);

                var info2 = new
                {
                    floorid = info.uid,
                    drawjson = info.DrawJsonStr,
                    id = info.id,
                };
                var jstr = Client.Put(url_floordrawingUpdate, Newtonsoft.Json.JsonConvert.SerializeObject(info2));
                var rstinfo = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(jstr);
                return (bool)rstinfo.success;
            }
            catch (Exception)
            {
                return false;
            }
        }
        internal static bool DeleteShape(string uid)
        {
            try
            {
                var jstr = Client.Post(url_shape_del + "?id=" + uid, "");
                var rstinfo = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(jstr);
                return (bool)rstinfo.success;
            }
            catch (Exception)
            {
                return false;
            }
        }


        private static string dynToStr(dynamic d)
        {
            if (d == null) return "";
            else return (string)d;
        }
    }
    public class ResponseInfo
    {
        public bool OK { get; set; }
        public string ErrorMsg { get; set; }
        public object ResultData { get; set; }
    }
}
